#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author : wzp
# date : modify by wzp 20220513

import sys
sys.path.append("./lib")
from flask import Flask,request
from WXBizMsgCrypt3 import WXBizMsgCrypt
from flask_restful import Api,reqparse
from Message import *



app = Flask(__name__)
api = Api(app)

# "POST /?msg_signature=6ece1db62ec129c41e6b5a3e776ac46a2decfd29&timestamp=1652588863&nonce=1653326668 HTTP/1.1"
api.add_resource(CMessage,'/zf<id>')
api.add_resource(CMessageStd,'/zf')

#正常回调
api.add_resource(Message,'/')

@app.before_request
def before_request():
    ip = request.remote_addr
    url = request.url
    form = request.form  # 请求的数据，可执行searchword = request.form.get('key', '')  测试（带参数的post请求）过程中form为空，不清楚原因
    args = request.args  # ?key=value，可执行searchword = request.args.get('key', '')
    values = request.values  # form和args的元组
    headers = request.headers
    method = request.method
    path = request.path
    base_url = request.base_url
    url_root = request.url_root
    data = request.data
    jsoninfo = request.json
    parser = reqparse.RequestParser()

    print("-------------para start-----------------")
    # print("ip", ip)
    # print("url", url)
    # print("form", form)
    # print("args", args)
    # print("values", values)
    # print("headers", headers)
    # print("method", method)
    # print("path", path)
    # print("base_url", base_url)
    # print("url_root", url_root)
    # print("data", data)
    # print("jsoninfo", jsoninfo)
    # print(args)
    print("--------------para end---------------")

#首次回调验证测试
#@app.route("/")
#def hello_world():
#    wxcpt = WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID)
#    args = request.args
#    msg_signature = args.get("msg_signature")
#    timestamp = args.get("timestamp")
#    nonce = args.get("nonce")
#    echostr = args.get("echostr")
#    ret, sEchoStr = wxcpt.VerifyURL(msg_signature,timestamp,nonce,echostr)
#    print(ret,sEchoStr)
#    return sEchoStr,200

# @app.route("/zf2")
# def resender2():
#     return request.path
#
# @app.route("/zf4")
# def resender4():
#     return request.path

if __name__ == "__main__":
    app.run(host="0.0.0.0",port=38080)
